﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DddCms.Domain;
using System.Data.Entity;
using DddCms.Domain.Repository;
using Microsoft.Practices.Unity;
namespace DddCms.Infrastructure.Repository
{
    public abstract class RepositoryItem<TEntity> : IRepositoryItem<TEntity> where TEntity : class
    {
        /// <summary>
        /// Is Code First context for acces ORM management
        /// </summary>
        protected IDataContext dataContext;

        public void setDataContext(object dataContext)
        {
            this.dataContext = (IDataContext)dataContext;
        }
        
        protected abstract IDbSet<TEntity> GetObjectQuery();

        public void Update(TEntity item)
        {
            dataContext.Save();
        }

        public void Add(TEntity item)
        {
            dataContext.ResolveInstance(item.GetType(), item);
            GetObjectQuery().Add(item);
            dataContext.Save();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(TEntity item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(TEntity[] array, int arrayIndex)
        {
            GetObjectQuery().ToList().CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return GetObjectQuery().Count(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(TEntity item)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<TEntity> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public Type ElementType
        {
            get { throw new NotImplementedException(); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { return GetObjectQuery().AsQueryable().Expression; }
        }

        public IQueryProvider Provider
        {
            get { return GetObjectQuery().AsQueryable().Provider; }
        }
    }
}
